package com.iwhalecloud.bss.kite.cucc.service.scene.loader.impl;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.iwhalecloud.bss.kite.cucc.client.api.scene.IInitSceneOfferInstService;
import com.iwhalecloud.bss.kite.cucc.client.dto.pos.KitePosDTO;
import com.iwhalecloud.bss.kite.cucc.service.addRoleProdDeal.AddRoleProdFactory;
import com.iwhalecloud.bss.kite.cucc.service.addRoleProdDeal.IAddRoleProdService;
import com.iwhalecloud.bss.kite.cucc.service.scene.loader.CuccInstLoader;
import com.iwhalecloud.bss.kite.dataservice.cache.OfferCache;
import com.iwhalecloud.bss.kite.dataservice.entity.spec.Offer;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInst;
import com.ztesoft.bss.common.bo.LoginInfo;

@Service
public class CuccAddRoleOfferInstLoader extends CuccInstLoader {
    
    @Autowired
    private IQuerySceneInstService querySceneInstService;
    
    @Autowired
    private IInitSceneOfferInstService initSceneOfferInstService;

    @Override
    public void loadInst(String sceneInstId, String operType, String custId, String staffId, KitePosDTO kitePosDTO, LoginInfo loginInfo) {
     // 为了不影响现有功能，对附属产品实例还原，增加了注解实现类，没有注解实现类的，走现有逻辑
        List<KiteOfferInst>  mainOfferInsts = this.querySceneInstService.listMainOfferInsts(sceneInstId);
        String catId = Optional.ofNullable(OfferCache.get(mainOfferInsts.get(0).getOfferId())).map(Offer::getCateId).orElse("");
        IAddRoleProdService addRoleProdService = AddRoleProdFactory.getRoleProdSupplier(catId);
        if (Objects.isNull(addRoleProdService)) {
            this.initSceneOfferInstService.initAddRoleOfferInst(sceneInstId, custId, staffId, kitePosDTO.getGoods(), loginInfo);
        }
        else {
            addRoleProdService.initAddRoleOfferInst(sceneInstId, custId, staffId, kitePosDTO.getGoods());
        }
    }

}
